#include <sys/mman.h>
#include <stdio.h>
#include <stdint.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "tiles.h"
#include "sprite.h"

void f_retro1(void *mmap) {
  uint64_t* ptr = mmap + 0x20;
  printf("mmap=%p mmap+0x20=%p $%lX ptr=$%08lX\n", mmap, mmap + 0x20, sizeof(void), *ptr);
}

void f_retro2(SDL_Surface *surfs[]) {
  int i;
  printf("void f_retro2@%p(SDL_Surface *surfs[]=%p)\n", &f_retro2, surfs);
  for(i = 0; i < 5 && surfs[i] != NULL; ++i) {
    printf("\tsurfs[$%X]=%p\n", i, surfs[i]);
  }
}

void f_changeSurfaceColour(SDL_Surface *s) {
  int ret;
  SDL_Color cols[] = {
    { 0x00, 0xff, 0x00},
    { 0x10, 0x55, 0x0e}
  };

  ret = SDL_SetColors(s, cols, 0, 2);
  printf("void f_changeSurfaceColour@%p(SDL_Surface *s=%p) = $%X\n", &f_changeSurfaceColour, s, ret);  
}

static char * my_xpm[] = {
  "20 10 3 1",
  "       c None",
  "-      c #ff0019",
  "#      c #77775E",
  "##-###### ----######",
  "##-###### ####--####",
  "##-###### ######-###",
  "##-#####   ######-##",
  "##-####     #####-##",
  "##-####     #####-##",
  "##-#####   ######-##",
  "###-##### #######-##",
  "####--### #######-##",
  "######--- #######-##"
};

void f_print_tests(R8B_Tiled_Screen *scr) {
  int i;
  char txt[] = "\x57""all work and no play makes jack a dull boy... ";

  SDL_Surface *mysurf = IMG_ReadXPMFromArray(my_xpm);
  R8B_Sprite *myspr = r8b_createSprite(0, &mysurf, 1, 50);
  printf("%p\t%p\n", mysurf, myspr);
  for(i = 0x20; i < 0x40; ++i) r8b_putCharScreen(scr, i);
  r8b_printStringScreenLn(scr, r8b_defaultCharConversionFun, "");
  r8b_printStringScreenLn(scr, r8b_c64CharConversionFun, "fine, fine, fine...");
  r8b_printStringScreenLn(scr, r8b_c64CharConversionFun, txt);
  for(i = 0; i < 20; ++i)
    r8b_printStringScreen(scr, r8b_c64CharConversionFun, txt);
  for(i = 0; i < 20; ++i)
    r8b_printStringScreenLn(scr, r8b_c64CharConversionFun, txt);
  for(i = 0; i < 3; ++i)
    r8b_printStringScreen(scr, r8b_c64CharConversionFun, txt);
  for(i = 0; i < 2; ++i)
    r8b_printStringScreenLn(scr, r8b_c64CharConversionFun, "-------------------");
  SDL_Flip(SDL_GetVideoSurface());
  //SDL_BlitSurface(mysurf, NULL, SDL_GetVideoSurface(), NULL);
  //SDL_Flip(SDL_GetVideoSurface());
  //SDL_Delay(1000);
  for(i = 0; i < 200; ++i) {
    SDL_Rect s = { 3*i, i, 100, 100 };
    SDL_Rect d = { 300, 100, 24, 24 };
    r8b_blitScreenPart(scr, &s, SDL_GetVideoSurface(), &d);
    SDL_Flip(SDL_GetVideoSurface());
    SDL_Delay(50);
  }
  /* printf("distance = %e\n", r8b_moveSpriteTo(myspr, 600, 400, 1.0/17)); */
  /* for(i = 0; i < 1000; ++i) { */
  /*   SDL_Delay(13); */
  /*   SDL_FillRect(SDL_GetVideoSurface(), NULL, 0); */
  /*   r8b_drawUpdateSprite(myspr, SDL_GetTicks(), SDL_GetVideoSurface()); */
  /*   SDL_Flip(SDL_GetVideoSurface()); */
  /* } */
  SDL_Delay(2000);
  SDL_FreeSurface(mysurf);
}
